package com.tnr.scgcxx.service.safty.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.tnr.scgcxx.dao.safty.RoleDao;
import com.tnr.scgcxx.dto.MenuDto;
import com.tnr.scgcxx.model.safty.Module;
import com.tnr.scgcxx.model.safty.Role;
import com.tnr.scgcxx.service.safty.RoleService;

@Service
public class RoleServiceImpl implements RoleService{

	@Autowired
	private RoleDao roleDao;

	@Override
	public List<Role> findRoles(Role role) {
		// TODO Auto-generated method stub
		return roleDao.findRoles(role);
	}

	@Override
	public void deleteRoleById(Integer role_id) {
		// TODO Auto-generated method stub
		roleDao.deleteRoleById(role_id);
	}

	@Override
	public int addRole(Role role) {
		// TODO Auto-generated method stub
		return roleDao.addRole(role);
	}

	@Override
	public void updateRoleStatuById(Integer role_id, String role_status) {
		// TODO Auto-generated method stub
		roleDao.updateRoleStatuById(role_id, role_status);
	}

	@Override
	public void updateRole(Role role) {
		// TODO Auto-generated method stub
		roleDao.updateRole(role);
	}

	@Override
	public int deleteRoles(Integer[] role_ids) {
		// TODO Auto-generated method stub
		return roleDao.deleteRoles(role_ids);
	}

	@Override
	public Role findRoleById(Integer role_id) {
		// TODO Auto-generated method stub
		return roleDao.findRoleById(role_id);
	}

	@Override
	public void updateRoleStatuByIds(Integer[] role_ids, String role_status) {
		// TODO Auto-generated method stub
		roleDao.updateRoleStatuByIds(role_ids, role_status);
	}

	@Override
	public List<MenuDto> getRoleModulesById(Integer ro_id) {
		List<Module> moduleList= roleDao.findRoleModulesById(ro_id);
		List<MenuDto>  menuDto  = new ArrayList<MenuDto>();
		MenuDto md = null;
		for(Module  m:moduleList){
			 if(md==null||!m.getP_id().equals(md.getMenuId())){
				md = new MenuDto();
				md.setMenuId(m.getP_id());
				md.setMenuName(m.getP_name());
				md.setSubMenuList(new ArrayList<MenuDto>());
				menuDto.add(md);
			 }
			 	
			 
			    MenuDto subMd = new MenuDto();
				 subMd.setMenuId(m.getM_id());
				 subMd.setMenuName(m.getM_name());
				 subMd.setMenuCheck(m.getRo_id()!=null); 
				 md.getSubMenuList().add(subMd);
				 
		}
		         return menuDto;
	}

	@Override
	public void setRoleModules(Integer[] m_ids, Integer ro_id) {
		roleDao.deleteModules(ro_id);
		if (m_ids!=null && m_ids.length!=0) {
			roleDao.addModules(m_ids,ro_id);
		}
		
	}
	
	
	


}
